package game.main;

import engine.app.View;
import engine.input.Key;
import game.controller.GameObject;
import game.tile.TileManager;

import java.util.ArrayList;
import java.util.Comparator;

import static game.basic.GameBase.*;
import static game.controller.GameObject.currentMap;
import static game.main.GameApp.player;

public class GamePanel extends View {

    public static int gameState;
    public static UI ui;
    TileManager tileM;
    public static AssetSetter aSetter;

    ArrayList<GameObject> entityList;

    public GamePanel() {
        super(screenWidth, screenHeight);
        entityList = new ArrayList<>();
        gameState = 0;
        ui = new UI(gc);
        tileM = new TileManager();
        aSetter = new AssetSetter();
        setupGame();
    }

    public void setupGame() {
        aSetter.setObject();
        aSetter.setNpc();
        aSetter.setMonster();
    }


    @Override
    public void onUpdate() {
        gc.clearRect(0, 0, screenWidth, screenHeight);
        switch (gameState) {
            case titleState -> ui.drawTitleScreen();
            case playState -> {
                player.update();
                drawGameObject();
                ui.drawPlayerHp();
                ui.drawMessage();
            }
            case pauseState -> {
                drawGameObject();
                ui.drawPauseScreen();
            }
            case dialogueState -> {
                drawGameObject();
                ui.drawDialogScreen();
                if (keyH.isPressed(Key.Enter) || keyH.isPressed(Key.Z)) {
                    gameState = playState;
                }
            }
            case characterState -> {
                drawGameObject();
                ui.drawCharacterScreen();
                ui.drawInventoryBag();
            }
        }
    }



    //绘制游戏画面
    private void drawGameObject() {
        tileM.drawMap(gc);

        entityList.add(player);

        for (int i = 0; i < objArrLength; i++) {
            if (obj[currentMap][i] != null) {
                entityList.add(obj[currentMap][i]);
            }
        }

        for (int i = 0; i < npcArrLength; i++) {
            if (npc[currentMap][i] != null) {
                if (gameState == playState) {
                    npc[currentMap][i].update();
                }
                entityList.add(npc[currentMap][i]);
            }
        }

        for (int i = 0; i < monsterArrLength; i++) {
            if (monster[currentMap][i] != null) {
                if (gameState == playState) {
                    monster[currentMap][i].update();
                    if (!monster[currentMap][i].alive) {
                        monster[currentMap][i] = null;
                    }
                }
                if (monster[currentMap][i] != null) {
                    entityList.add(monster[currentMap][i]);
                }
            }
        }

        entityList.sort(Comparator.comparingInt(e -> e.worldY));
        for (GameObject gameObject : entityList) {
            gameObject.draw(gc);
        }
        entityList.clear();
    }
}


//更新游戏画面 分离
//    private void updateGameObject() {
//        player.update();
//        for (int i = 0; i < npcArrLength; i++) {
//            if (npc[currentMap][i] != null) {
//                npc[currentMap][i].update();
//            }
//        }
//        for (int i = 0; i < monsterArrLength; i++) {
//            if (monster[currentMap][i] != null) {
//                if (monster[currentMap][i].alive && !monster[currentMap][i].dying) {
//                    monster[currentMap][i].update();
//                } else if (!monster[currentMap][i].alive) {
//                    monster[currentMap][i] = null;
//                }
//            }
//        }
//    }
